/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import com.sun.lwuit.Command;
import com.sun.lwuit.Display;
import com.sun.lwuit.Form;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.plaf.Border;
import core.MediaManager;
import core.MessageFormPool;
import core.ScreenEngine;
import core.Settings;
import datamodel.Message;
import view.commands.CenterCommand;

/**
 *
 * @author Kvest
 */
public class MessageForm extends Form
{
    public static final int OK_ID = 1;
    
    private static final int TITLE_BG_COLOR = 0x32CD32;
    private static final int TITLE_FONT_COLOR = 0x0;
    
    private TextArea messageText;
   
    private Command ok;
    private CenterCommand centerCommand;
    
    public MessageForm()
    {
        super();
  
        //Цвет заголовка
        getTitleComponent().getStyle().setBgColor(TITLE_BG_COLOR);
        getTitleComponent().getStyle().setFgColor(TITLE_FONT_COLOR);
        
        init();
    }
    
    private void init()
    {
        setLayout(new BorderLayout());

        messageText = new TextArea("", 1, 30);
        messageText.setEditable(false);
        messageText.setGrowByContent(true);
        
        Border border = Border.createLineBorder(3, TITLE_BG_COLOR);
        messageText.getSelectedStyle().setBorder(border);     
        messageText.getSelectedStyle().setPadding(3, 3, 6, 6);
        
        addComponent(BorderLayout.CENTER, messageText);
        
        ok = new Command("ok",OK_ID)
        {
            public void actionPerformed(ActionEvent evt)
            {
                //Отключаем музыку
                if (MediaManager.getManager().getCurrentMediaID() == Settings.getSettings().getMessageMelodyId())
                {
                    MediaManager.getManager().stopMusic();
                }
                        
                //Закрываем форму
                ScreenEngine.getEngine().pop(true);

                //Возвращаем форму в пул
                MessageFormPool.getPool().dispose(MessageForm.this);
                
                evt.consume();
            }
        };
        centerCommand = new CenterCommand(ok);
        
        if (!Display.getInstance().isTouchScreenDevice())
        {
            addCommand(centerCommand);
        }    
        addCommand(ok);
    }
    
    public void setContent(Message message)
    {
        setTitle(message.getTitle());
        messageText.setText(message.getText());
    }
}
